package top.zosoft.handler;

import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.mqtt.*;
import lombok.extern.slf4j.Slf4j;
import top.zosoft.annotation.Process;

/**
 * CONNCET 回复类
 * @author neal
 * @date 2023/12/20  16:16
 */
@Slf4j
@Process(type=MqttMessageType.CONNECT)
public class MqttConnect implements MqttHandler {

    @Override
    public void handler(ChannelHandlerContext ctx, MqttMessage msg) {
        /**回复:CONNACK(2)
         * Fixed Header:00000010 00000000
         * Variable Header:packetID(messageID)
         */
        log.debug("收到CONNECT请求");
        MqttConnectMessage connMessage = (MqttConnectMessage) msg;
        MqttFixedHeader fixedHeader = new MqttFixedHeader(MqttMessageType.CONNACK, false, MqttQoS.AT_MOST_ONCE, false, 2);
        MqttConnAckVariableHeader variableHeader = new MqttConnAckVariableHeader(MqttConnectReturnCode.CONNECTION_ACCEPTED, false);
        MqttConnAckMessage message = new MqttConnAckMessage(fixedHeader, variableHeader);
        ctx.writeAndFlush(message);
    }
}
